home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / Pict2Ascii 1.03 / Src / CPreferences.cp next >
Encoding:
Text File  |  1997-07-09  |  6.9 KB  |  233 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    CPreferences.cp                                ©1997 BB's Team inc. All rights reserved
  3. // =================================================================================
  4. #include "CPreferences.h"
  5. #include "PLConstants.h"
  6.  
  7. #include "CDynamicEditField.h"
  8. #include <UModalDialogs.h>
  9. #include <UResourceMgr.h>
  10. #include <LWindow.h>
  11.  
  12.  
  13. // ---------------------------------------------------------------------------------
  14. //        • WritePrefs
  15. // ---------------------------------------------------------------------------------
  16. void CPreferences::WritePrefs (void)
  17. {
  18.     
  19.     Try_ {
  20.         // open rsrc
  21.         mFile.OpenOrCreateResourceFork (fsWrPerm, kSignature, kPrefsFileType, nil);
  22.  
  23.         // Change existing / Create new (StNewResource has a (Handle) accessor)
  24.         {
  25.             StNewResource rsrcHandle (kPrefsRsrcType, kPrefsRsrcID, sizeof (Prefs), true);
  26.             BlockMoveData (&mPrefs, *(Handle)rsrcHandle, sizeof(Prefs));
  27.             // ~StNewResource writes out the resource
  28.         }
  29.         
  30.         // leave the rsrc fork closed
  31.         mFile.CloseResourceFork();
  32.     }
  33.  
  34.     Catch_ (inErr) {
  35.     } EndCatch_
  36. }
  37.  
  38.  
  39. // ---------------------------------------------------------------------------------
  40. //        • ReadPrefs
  41. // ---------------------------------------------------------------------------------
  42. void CPreferences::ReadPrefs (void)
  43. {
  44.     Try_ {
  45.         // open rsrc
  46.         mFile.OpenResourceFork (fsRdPerm);
  47.  
  48.         // load rsrc if exists
  49.         {
  50.             StResource rsrcHandle (kPrefsRsrcType, kPrefsRsrcID, true, true);
  51.             BlockMoveData (*(Handle)rsrcHandle, &mPrefs, sizeof(Prefs));
  52.         }
  53.  
  54.         // leave the rsrc fork closed
  55.         mFile.CloseResourceFork();
  56.     }
  57.  
  58.     Catch_ (inErr) {
  59.     } EndCatch_
  60. }
  61.  
  62.  
  63. // ---------------------------------------------------------------------------------
  64. //        • ctor
  65. // ---------------------------------------------------------------------------------
  66. CPreferences::CPreferences()
  67.     : mFile (kPrefsFileName)
  68. {
  69.     mPrefs.sTextTraits.size = 255;
  70.     mPrefs.sScreen = mPrefs.s7Bits
  71.                         = mPrefs.sContrast
  72.                    = mPrefs.sConfirmPrinting
  73.                    = mPrefs.sMonoSpace
  74.                    = mPrefs.sBeep32k
  75.                    = 255;
  76.     AllowAll (mPrefs.sAlphabet);
  77.     
  78.     ReadPrefs();
  79. }
  80.  
  81.  
  82. // ---------------------------------------------------------------------------------
  83. //        • FinishCreate
  84. // ---------------------------------------------------------------------------------
  85. void CPreferences::FinishCreate(void)
  86. {
  87.     // retrieve default values from the PPob resources
  88.     // cannot be done in the ctor : PPob classes are not registered yet !
  89.     if ( !IsOk() ) {
  90.         StDialogHandler theHandler (rPPob_PrefsDialog, nil);
  91.         LWindow *theDialog = theHandler.GetDialog();
  92.         Assert_ (theDialog != nil);
  93.         mPrefs.sConfirmPrinting    = theDialog->GetValueForPaneID (kConfirmCheck);
  94.         mPrefs.sMonoSpace            = theDialog->GetValueForPaneID (kMonoTestCheck);
  95.         mPrefs.sBeep32k            = theDialog->GetValueForPaneID (kBeep32k);
  96.     }
  97. }
  98.  
  99.  
  100. // ---------------------------------------------------------------------------------
  101. //        • dtor
  102. // ---------------------------------------------------------------------------------
  103. CPreferences::~CPreferences()
  104. {
  105.     WritePrefs();
  106. }
  107.  
  108.  
  109. // ---------------------------------------------------------------------------------
  110. //        • AllowAll
  111. // ---------------------------------------------------------------------------------
  112. void CPreferences::AllowAll (Str255 &inAllow)
  113. {
  114.     LStr255 allow;
  115.     allow = "\p";
  116.  
  117.     // word wrap in the LEditField makes a CR where the space lies
  118.     // let's take advantage of this to distinguish between 7 and 8 bits ascii
  119.     for (Int16 i = kFirstChar+1 ; i<128 ; i++)
  120.         allow += (Uchar) i;
  121.     allow += (Uchar) kFirstChar;
  122.     for (Int16 i = 128 ; i<256 ; i++)
  123.         allow += (Uchar) i;
  124.     
  125.     // but forbid from the 'Excl' rsrc (at least char 127 is a problem)
  126.     Handle theExcl = ::GetResource ('Excl', 128);
  127.     if (theExcl) {
  128.     
  129.         ::HLock (theExcl);
  130.         
  131.         allow.SetCompareFunc(LString::CompareBytes);
  132.         Int16 count = *(Int16*)(*theExcl);        // first one 16 bits : the size
  133.         Uchar *array = (Uchar*)(*theExcl);        // then 8 bits.
  134.         
  135.         for (Int16 i=2 ; i<=1+count ; ++i) {                // shift 1 right for the size
  136.             Uint8 found = allow.Find (array+i, 1, 1);        // array+i : address of the i th char
  137.             if (found)
  138.                 allow.Remove (found, 1);
  139.         }
  140.  
  141.         ::HUnlock (theExcl);
  142.         ::ReleaseResource ( theExcl );
  143.     }
  144.  
  145.     LString::CopyPStr (allow, inAllow);
  146.     
  147. }
  148.  
  149.  
  150. // ---------------------------------------------------------------------------------
  151. //        • IsOk
  152. // ---------------------------------------------------------------------------------
  153. Boolean CPreferences::IsOk(void)
  154. {
  155.     return    mPrefs.sTextTraits.size    != 255
  156.            && mPrefs.sScreen                != 255
  157.            && mPrefs.s7Bits                    != 255
  158.            && mPrefs.sContrast                != 255
  159.            && mPrefs.sConfirmPrinting    != 255
  160.            && mPrefs.sMonoSpace            != 255
  161.            && mPrefs.sBeep32k                != 255;
  162. }
  163.  
  164.  
  165. // ---------------------------------------------------------------------------------
  166. //        • GetTextTraits
  167. // ---------------------------------------------------------------------------------
  168. void CPreferences::GetTextTraits (TextTraitsRecord &outTextTraits) const
  169. {
  170.     ::BlockMoveData( &mPrefs.sTextTraits, &outTextTraits, sizeof(TextTraitsRecord) );
  171. }
  172.  
  173.  
  174. // ---------------------------------------------------------------------------------
  175. //        • SetTextTraits
  176. // ---------------------------------------------------------------------------------
  177. void CPreferences::SetTextTraits (const TextTraitsRecord    &inTextTraits)
  178. {
  179.     ::BlockMoveData( &inTextTraits, &mPrefs.sTextTraits, sizeof(TextTraitsRecord) );
  180. }
  181.  
  182.  
  183. // ---------------------------------------------------------------------------------
  184. //        • DoDialog
  185. // ---------------------------------------------------------------------------------
  186. void CPreferences::DoDialog (LStr255 inAllowed, const Str255 displayFont)
  187. {
  188.     // no need to bother with a commander
  189.     StDialogHandler theHandler (rPPob_PrefsDialog, nil);
  190.  
  191.     // Get the dialog.
  192.     LWindow *theDialog = theHandler.GetDialog();
  193.     Assert_ (theDialog != nil);
  194.  
  195.     // setup controls
  196.     theDialog->SetValueForPaneID         (kConfirmCheck        , mPrefs.sConfirmPrinting);
  197.     theDialog->SetValueForPaneID         (kMonoTestCheck    , mPrefs.sMonoSpace);
  198.     theDialog->SetValueForPaneID        (kBeep32k            , mPrefs.sBeep32k);
  199.  
  200.     CDynamicEditField *theField = (CDynamicEditField*) theDialog->FindPaneByID (kCharsEditField);
  201.     Assert_ (theField != nil);
  202.     theField->SetDescriptor (inAllowed);
  203.     if (*displayFont)
  204.         theField->SetFontName (displayFont);
  205.     theDialog->Show();
  206.     
  207.     while (true) {
  208.  
  209.         MessageT theMsg = theHandler.DoDialog();
  210.  
  211.         // Handle cancel message.
  212.         if (theMsg == msg_CancelButton)
  213.             break;
  214.  
  215.         // Handle OK message.
  216.         else if (theMsg == msg_OkButton) {    
  217.             mPrefs.sConfirmPrinting    = theDialog->GetValueForPaneID (kConfirmCheck);
  218.             mPrefs.sMonoSpace            = theDialog->GetValueForPaneID (kMonoTestCheck);
  219.             mPrefs.sBeep32k                = theDialog->GetValueForPaneID (kBeep32k);
  220.             theDialog->GetDescriptorForPaneID (kCharsEditField, mPrefs.sAlphabet);
  221.             break;
  222.         }
  223.  
  224.         else if (theMsg == msg_Reset) {
  225.             Str255 all;
  226.             AllowAll (all);
  227.             theDialog->SetDescriptorForPaneID(kCharsEditField, all);
  228.         }
  229.         
  230.     }    // while
  231.     
  232. }
  233.